<?php

/*
 * Copyright 2018 zhuo bing.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace backend\components\widgets\region;

use yii\base\Widget;
use backend\models\Region;
use yii\helpers\ArrayHelper;
use Yii;
use yii\helpers\Url;
/**
 * Description of RegionWidget
 *
 * @author zhuo bing
 */
class RegionWidget extends Widget {

    public $pid = 1;
    public $model;
    protected $name;
    public function init(){
        $this->name = explode('\\',get_class($this->model));
        $this->name = $this->name[count($this->name)-1].'[region_id]';            
    }
    public function run() {
        
        $this->renderJs();
        if ($this->model and $this->model->region_id) {
            $currRegion = Region::findOne(['region_id' => $this->model->region_id]);
            $select = '<div class="col-lg-2">'.$this->renderSelect($currRegion['parent'], $currRegion['region_id']).'</div>';
        }
        
        $regions = Region::getCurrSubNode($this->pid);
        $regions = ArrayHelper::map($regions, 'region_id', 'region_name');
        $html = $this->select($regions);
        $label = Yii::t('common','Region');
        return <<<html
        <div class="form-group region">
            <label class="col-lg-2 control-label">$label</label>
            <div class="col-lg-2">$html</div>$select
        </div>
html;
    }
    /**
     * 返回指定id的子地区
     * @param type $pid
     * @return type
     */
    public function renderSelect($pid,$checkedId = 0){
        $regions = Region::getCurrSubNode($pid);
        if($regions){
            $regions = ArrayHelper::map($regions, 'region_id', 'region_name');
            return $html = $this->select($regions, $checkedId);
        }
    }
    
    static public function renderData($pid){
        $regions = Region::getCurrSubNode($pid);
        if($regions){
            return $regions = ArrayHelper::map($regions, 'region_id', 'region_name');
        }
    }
    
    protected function renderJs(){
        $url = Url::to(['region-select']);
        $view = $this->getView();
        $js = <<<js
                $('.region').on('change','select',function(){
                    var pid = $(this).val();
                    var othis = $(this);
                    if(pid){
                        $.post(
                            '$url',
                            {pid:pid},
                            function(json){
                                if(json.result == '1'){
                                    
                                    var index = othis.parent().index();
                                    $('.region div').each(function () {
                                        if ($(this).index() > index) {
                                            $(this).remove();
                                        }
                                    });    
                                    $('.region select').removeAttr('name');
                                    
                                    var select = '<div class="col-lg-2"><select class="form-control" name="$this->name"><option value="">请选择</option>'
                                    for(x in json.select){
                                        select+='<option value="'+x+'">'+json.select[x]+'</option>'
                                    }
                                    select+="</select></div>";
                                   console.log(select);
                                    othis.parents('.region').append(select);
                                    select.children().attr('name','$this->name');
                                }
                            },'json'
                        );
                    }
                });
js;
        $view->registerJs($js);
    }
    public function select($data, $checkedId = 0){
        $options = "<option>请选择...</option>";

        foreach($data as $k=>$v){
            $checked = '';
            if($k == $checkedId) $checked = 'selected';
            $options .= "<option $checked value='$k' >$v</option>";
        }
        return $select = '<select class="form-control" name="'.$this->name.'">'.$options.'</select>';
    }
}
